package com.security.controller;

import com.entity.User;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.time.Instant;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/template")
public class TemplateController {
    @Autowired
    StringRedisTemplate stringRedisTemplate;
    @Autowired
    RedisTemplate redisTemplate;
    @Autowired
    ObjectMapper objectMapper;

    @GetMapping("/map")
    public Map testMap() {
        Map<String, Object> map = new HashMap<>();
        map.put("m1", 1234);
        map.put("m2", "string");
        map.put("m3", Instant.now());
        redisTemplate.opsForValue().set("map", map);
        return (Map) redisTemplate.opsForValue().get("map");
    }

    @GetMapping("/object")
    public User user() throws IOException {
        User user = new User("uuid1001", "admin", "123456");
        redisTemplate.opsForValue().set("user", user);
        // todo: 无法反序列化成对象
        redisTemplate.opsForValue().get("user");
//        return saveUser;
        return null;
    }
}
